

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>optuna.integration.cma &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorial/index.html">教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">模块代码</a> &raquo;</li>
        
      <li>optuna.integration.cma</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>optuna.integration.cma 源代码</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">random</span>

<span class="kn">import</span> <span class="nn">numpy</span>

<span class="kn">import</span> <span class="nn">optuna</span>
<span class="kn">from</span> <span class="nn">optuna._imports</span> <span class="kn">import</span> <span class="n">try_import</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">distributions</span>
<span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">CategoricalDistribution</span>
<span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">DiscreteUniformDistribution</span>
<span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">IntUniformDistribution</span>
<span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">LogUniformDistribution</span>
<span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">UniformDistribution</span>
<span class="kn">from</span> <span class="nn">optuna.samplers</span> <span class="kn">import</span> <span class="n">BaseSampler</span>
<span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">StudyDirection</span>
<span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">TrialState</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">type_checking</span>

<span class="k">with</span> <span class="n">try_import</span><span class="p">()</span> <span class="k">as</span> <span class="n">_imports</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">cma</span>

<span class="k">if</span> <span class="n">type_checking</span><span class="o">.</span><span class="n">TYPE_CHECKING</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Set</span>  <span class="c1"># NOQA</span>

    <span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">BaseDistribution</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">FrozenTrial</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">Study</span>  <span class="c1"># NOQA</span>

<span class="c1"># Minimum value of sigma0 to avoid ZeroDivisionError in cma.CMAEvolutionStrategy.</span>
<span class="n">_MIN_SIGMA0</span> <span class="o">=</span> <span class="mf">1e-10</span>


<div class="viewcode-block" id="CmaEsSampler"><a class="viewcode-back" href="../../../reference/integration.html#optuna.integration.CmaEsSampler">[文档]</a><span class="k">class</span> <span class="nc">CmaEsSampler</span><span class="p">(</span><span class="n">BaseSampler</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A Sampler using cma library as the backend.</span>

<span class="sd">    Example:</span>

<span class="sd">        Optimize a simple quadratic function by using :class:`~optuna.integration.CmaEsSampler`.</span>

<span class="sd">        .. testcode::</span>

<span class="sd">            import optuna</span>

<span class="sd">            def objective(trial):</span>
<span class="sd">                x = trial.suggest_uniform(&#39;x&#39;, -1, 1)</span>
<span class="sd">                y = trial.suggest_int(&#39;y&#39;, -1, 1)</span>
<span class="sd">                return x**2 + y</span>

<span class="sd">            sampler = optuna.integration.CmaEsSampler()</span>
<span class="sd">            study = optuna.create_study(sampler=sampler)</span>
<span class="sd">            study.optimize(objective, n_trials=20)</span>

<span class="sd">    Note that parallel execution of trials may affect the optimization performance of CMA-ES,</span>
<span class="sd">    especially if the number of trials running in parallel exceeds the population size.</span>

<span class="sd">    Args:</span>

<span class="sd">        x0:</span>
<span class="sd">            A dictionary of an initial parameter values for CMA-ES. By default, the mean of ``low``</span>
<span class="sd">            and ``high`` for each distribution is used.</span>
<span class="sd">            Please refer to cma.CMAEvolutionStrategy_ for further details of ``x0``.</span>

<span class="sd">        sigma0:</span>
<span class="sd">            Initial standard deviation of CMA-ES. By default, ``sigma0`` is set to</span>
<span class="sd">            ``min_range / 6``, where ``min_range`` denotes the minimum range of the distributions</span>
<span class="sd">            in the search space. If distribution is categorical, ``min_range`` is</span>
<span class="sd">            ``len(choices) - 1``.</span>
<span class="sd">            Please refer to cma.CMAEvolutionStrategy_ for further details of ``sigma0``.</span>

<span class="sd">        cma_stds:</span>
<span class="sd">            A dictionary of multipliers of sigma0 for each parameters. The default value is 1.0.</span>
<span class="sd">            Please refer to cma.CMAEvolutionStrategy_ for further details of ``cma_stds``.</span>

<span class="sd">        seed:</span>
<span class="sd">            A random seed for CMA-ES.</span>

<span class="sd">        cma_opts:</span>
<span class="sd">            Options passed to the constructor of cma.CMAEvolutionStrategy_ class.</span>

<span class="sd">            Note that ``BoundaryHandler``, ``bounds``, ``CMA_stds`` and ``seed`` arguments in</span>
<span class="sd">            ``cma_opts`` will be ignored because it is added by</span>
<span class="sd">            :class:`~optuna.integration.CmaEsSampler` automatically.</span>

<span class="sd">        n_startup_trials:</span>
<span class="sd">            The independent sampling is used instead of the CMA-ES algorithm until the given number</span>
<span class="sd">            of trials finish in the same study.</span>

<span class="sd">        independent_sampler:</span>
<span class="sd">            A :class:`~optuna.samplers.BaseSampler` instance that is used for independent</span>
<span class="sd">            sampling. The parameters not contained in the relative search space are sampled</span>
<span class="sd">            by this sampler.</span>
<span class="sd">            The search space for :class:`~optuna.integration.CmaEsSampler` is determined by</span>
<span class="sd">            :func:`~optuna.samplers.intersection_search_space()`.</span>

<span class="sd">            If :obj:`None` is specified, :class:`~optuna.samplers.RandomSampler` is used</span>
<span class="sd">            as the default.</span>

<span class="sd">            .. seealso::</span>
<span class="sd">                :class:`optuna.samplers` module provides built-in independent samplers</span>
<span class="sd">                such as :class:`~optuna.samplers.RandomSampler` and</span>
<span class="sd">                :class:`~optuna.samplers.TPESampler`.</span>

<span class="sd">        warn_independent_sampling:</span>
<span class="sd">            If this is :obj:`True`, a warning message is emitted when</span>
<span class="sd">            the value of a parameter is sampled by using an independent sampler.</span>

<span class="sd">            Note that the parameters of the first trial in a study are always sampled</span>
<span class="sd">            via an independent sampler, so no warning messages are emitted in this case.</span>

<span class="sd">    .. _cma.CMAEvolutionStrategy: http://cma.gforge.inria.fr/apidocs-pycma/\</span>
<span class="sd">    cma.evolution_strategy.CMAEvolutionStrategy.html</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">x0</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[Dict[str, Any]]</span>
        <span class="n">sigma0</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[float]</span>
        <span class="n">cma_stds</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[Dict[str, float]]</span>
        <span class="n">seed</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[int]</span>
        <span class="n">cma_opts</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[Dict[str, Any]]</span>
        <span class="n">n_startup_trials</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>  <span class="c1"># type: int</span>
        <span class="n">independent_sampler</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[BaseSampler]</span>
        <span class="n">warn_independent_sampling</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>  <span class="c1"># type: bool</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; None</span>

        <span class="n">_imports</span><span class="o">.</span><span class="n">check</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_x0</span> <span class="o">=</span> <span class="n">x0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sigma0</span> <span class="o">=</span> <span class="n">sigma0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_cma_stds</span> <span class="o">=</span> <span class="n">cma_stds</span>
        <span class="k">if</span> <span class="n">seed</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">seed</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span> <span class="o">**</span> <span class="mi">32</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_cma_opts</span> <span class="o">=</span> <span class="n">cma_opts</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_cma_opts</span><span class="p">[</span><span class="s2">&quot;seed&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">seed</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_cma_opts</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s2">&quot;verbose&quot;</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_n_startup_trials</span> <span class="o">=</span> <span class="n">n_startup_trials</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_independent_sampler</span> <span class="o">=</span> <span class="n">independent_sampler</span> <span class="ow">or</span> <span class="n">optuna</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">RandomSampler</span><span class="p">(</span><span class="n">seed</span><span class="o">=</span><span class="n">seed</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_warn_independent_sampling</span> <span class="o">=</span> <span class="n">warn_independent_sampling</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">logging</span><span class="o">.</span><span class="n">get_logger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_search_space</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">IntersectionSearchSpace</span><span class="p">()</span>

<div class="viewcode-block" id="CmaEsSampler.reseed_rng"><a class="viewcode-back" href="../../../reference/integration.html#optuna.integration.CmaEsSampler.reseed_rng">[文档]</a>    <span class="k">def</span> <span class="nf">reseed_rng</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_cma_opts</span><span class="p">[</span><span class="s2">&quot;seed&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span> <span class="o">**</span> <span class="mi">32</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_independent_sampler</span><span class="o">.</span><span class="n">reseed_rng</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="nf">infer_relative_search_space</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">):</span>
        <span class="c1"># type: (Study, FrozenTrial) -&gt; Dict[str, BaseDistribution]</span>

        <span class="n">search_space</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">distribution</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_search_space</span><span class="o">.</span><span class="n">calculate</span><span class="p">(</span><span class="n">study</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">distribution</span><span class="o">.</span><span class="n">single</span><span class="p">():</span>
                <span class="c1"># `cma` cannot handle distributions that contain just a single value, so we skip</span>
                <span class="c1"># them. Note that the parameter values for such distributions are sampled in</span>
                <span class="c1"># `Trial`.</span>
                <span class="k">continue</span>

            <span class="n">search_space</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">distribution</span>

        <span class="k">return</span> <span class="n">search_space</span>

    <span class="k">def</span> <span class="nf">sample_independent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_distribution</span><span class="p">):</span>
        <span class="c1"># type: (Study, FrozenTrial, str, BaseDistribution) -&gt; float</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_warn_independent_sampling</span><span class="p">:</span>
            <span class="n">complete_trials</span> <span class="o">=</span> <span class="p">[</span><span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">study</span><span class="o">.</span><span class="n">trials</span> <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">state</span> <span class="o">==</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">COMPLETE</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">complete_trials</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_n_startup_trials</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_log_independent_sampling</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">param_name</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_independent_sampler</span><span class="o">.</span><span class="n">sample_independent</span><span class="p">(</span>
            <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_distribution</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">sample_relative</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">search_space</span><span class="p">):</span>
        <span class="c1"># type: (Study, FrozenTrial, Dict[str, BaseDistribution]) -&gt; Dict[str, float]</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">search_space</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">{}</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">search_space</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                <span class="s2">&quot;`CmaEsSampler` does not support optimization of 1-D search space. &quot;</span>
                <span class="s2">&quot;`</span><span class="si">{}</span><span class="s2">` is used instead of `CmaEsSampler`.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_independent_sampler</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span>
                <span class="p">)</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_warn_independent_sampling</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="k">return</span> <span class="p">{}</span>

        <span class="n">complete_trials</span> <span class="o">=</span> <span class="p">[</span><span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">study</span><span class="o">.</span><span class="n">trials</span> <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">state</span> <span class="o">==</span> <span class="n">TrialState</span><span class="o">.</span><span class="n">COMPLETE</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">complete_trials</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_n_startup_trials</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">{}</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x0</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_x0</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_initialize_x0</span><span class="p">(</span><span class="n">search_space</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sigma0</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">sigma0</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_initialize_sigma0</span><span class="p">(</span><span class="n">search_space</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sigma0</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sigma0</span>
        <span class="n">sigma0</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">sigma0</span><span class="p">,</span> <span class="n">_MIN_SIGMA0</span><span class="p">)</span>

        <span class="n">optimizer</span> <span class="o">=</span> <span class="n">_Optimizer</span><span class="p">(</span><span class="n">search_space</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x0</span><span class="p">,</span> <span class="n">sigma0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cma_stds</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cma_opts</span><span class="p">)</span>
        <span class="n">trials</span> <span class="o">=</span> <span class="n">study</span><span class="o">.</span><span class="n">trials</span>
        <span class="n">last_told_trial_number</span> <span class="o">=</span> <span class="n">optimizer</span><span class="o">.</span><span class="n">tell</span><span class="p">(</span><span class="n">trials</span><span class="p">,</span> <span class="n">study</span><span class="o">.</span><span class="n">direction</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">optimizer</span><span class="o">.</span><span class="n">ask</span><span class="p">(</span><span class="n">trials</span><span class="p">,</span> <span class="n">last_told_trial_number</span><span class="p">)</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_initialize_x0</span><span class="p">(</span><span class="n">search_space</span><span class="p">):</span>
        <span class="c1"># type: (Dict[str, BaseDistribution]) -&gt; Dict[str, Any]</span>

        <span class="n">x0</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">distribution</span> <span class="ow">in</span> <span class="n">search_space</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="c1"># TODO(nzw0301) support IntLogUniform</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">distribution</span><span class="p">,</span> <span class="n">UniformDistribution</span><span class="p">):</span>
                <span class="n">x0</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">mean</span><span class="p">([</span><span class="n">distribution</span><span class="o">.</span><span class="n">high</span><span class="p">,</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span><span class="p">])</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">distribution</span><span class="p">,</span> <span class="n">DiscreteUniformDistribution</span><span class="p">):</span>
                <span class="n">x0</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">mean</span><span class="p">([</span><span class="n">distribution</span><span class="o">.</span><span class="n">high</span><span class="p">,</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span><span class="p">])</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">distribution</span><span class="p">,</span> <span class="n">IntUniformDistribution</span><span class="p">):</span>
                <span class="n">x0</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">mean</span><span class="p">([</span><span class="n">distribution</span><span class="o">.</span><span class="n">high</span><span class="p">,</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span><span class="p">]))</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">distribution</span><span class="p">,</span> <span class="n">LogUniformDistribution</span><span class="p">):</span>
                <span class="n">log_high</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">distribution</span><span class="o">.</span><span class="n">high</span><span class="p">)</span>
                <span class="n">log_low</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">distribution</span><span class="o">.</span><span class="n">low</span><span class="p">)</span>
                <span class="n">x0</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">mean</span><span class="p">([</span><span class="n">log_high</span><span class="p">,</span> <span class="n">log_low</span><span class="p">]))</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">distribution</span><span class="p">,</span> <span class="n">CategoricalDistribution</span><span class="p">):</span>
                <span class="n">index</span> <span class="o">=</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">distribution</span><span class="o">.</span><span class="n">choices</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span>
                <span class="n">x0</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">choices</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
                    <span class="s2">&quot;The distribution </span><span class="si">{}</span><span class="s2"> is not implemented.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">distribution</span><span class="p">)</span>
                <span class="p">)</span>
        <span class="k">return</span> <span class="n">x0</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_initialize_sigma0</span><span class="p">(</span><span class="n">search_space</span><span class="p">):</span>
        <span class="c1"># type: (Dict[str, BaseDistribution]) -&gt; float</span>

        <span class="n">sigma0s</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">distribution</span> <span class="ow">in</span> <span class="n">search_space</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="c1"># TODO(nzw0301) support IntLogUniform</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">distribution</span><span class="p">,</span> <span class="n">UniformDistribution</span><span class="p">):</span>
                <span class="n">sigma0s</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">distribution</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span><span class="p">)</span> <span class="o">/</span> <span class="mi">6</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">distribution</span><span class="p">,</span> <span class="n">DiscreteUniformDistribution</span><span class="p">):</span>
                <span class="n">sigma0s</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">distribution</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span><span class="p">)</span> <span class="o">/</span> <span class="mi">6</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">distribution</span><span class="p">,</span> <span class="n">IntUniformDistribution</span><span class="p">):</span>
                <span class="n">sigma0s</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">distribution</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span><span class="p">)</span> <span class="o">/</span> <span class="mi">6</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">distribution</span><span class="p">,</span> <span class="n">LogUniformDistribution</span><span class="p">):</span>
                <span class="n">log_high</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">distribution</span><span class="o">.</span><span class="n">high</span><span class="p">)</span>
                <span class="n">log_low</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">distribution</span><span class="o">.</span><span class="n">low</span><span class="p">)</span>
                <span class="n">sigma0s</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">log_high</span> <span class="o">-</span> <span class="n">log_low</span><span class="p">)</span> <span class="o">/</span> <span class="mi">6</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">distribution</span><span class="p">,</span> <span class="n">CategoricalDistribution</span><span class="p">):</span>
                <span class="n">sigma0s</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">distribution</span><span class="o">.</span><span class="n">choices</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">6</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
                    <span class="s2">&quot;The distribution </span><span class="si">{}</span><span class="s2"> is not implemented.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">distribution</span><span class="p">)</span>
                <span class="p">)</span>
        <span class="k">return</span> <span class="nb">min</span><span class="p">(</span><span class="n">sigma0s</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_log_independent_sampling</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">param_name</span><span class="p">):</span>
        <span class="c1"># type: (FrozenTrial, str) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
            <span class="s2">&quot;The parameter &#39;</span><span class="si">{}</span><span class="s2">&#39; in trial#</span><span class="si">{}</span><span class="s2"> is sampled independently &quot;</span>
            <span class="s2">&quot;by using `</span><span class="si">{}</span><span class="s2">` instead of `CmaEsSampler` &quot;</span>
            <span class="s2">&quot;(optimization performance may be degraded). &quot;</span>
            <span class="s2">&quot;You can suppress this warning by setting `warn_independent_sampling` &quot;</span>
            <span class="s2">&quot;to `False` in the constructor of `CmaEsSampler`, &quot;</span>
            <span class="s2">&quot;if this independent sampling is intended behavior.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">param_name</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">number</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_independent_sampler</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span>
            <span class="p">)</span>
        <span class="p">)</span></div>


<span class="k">class</span> <span class="nc">_Optimizer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">search_space</span><span class="p">,</span>  <span class="c1"># type: Dict[str, BaseDistribution]</span>
        <span class="n">x0</span><span class="p">,</span>  <span class="c1"># type: Dict[str, Any]</span>
        <span class="n">sigma0</span><span class="p">,</span>  <span class="c1"># type: float</span>
        <span class="n">cma_stds</span><span class="p">,</span>  <span class="c1"># type: Optional[Dict[str, float]]</span>
        <span class="n">cma_opts</span><span class="p">,</span>  <span class="c1"># type: Dict[str, Any]</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_search_space</span> <span class="o">=</span> <span class="n">search_space</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_param_names</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_search_space</span><span class="o">.</span><span class="n">keys</span><span class="p">()))</span>

        <span class="n">lows</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">highs</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">param_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_param_names</span><span class="p">:</span>
            <span class="n">dist</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_search_space</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist</span><span class="p">,</span> <span class="n">CategoricalDistribution</span><span class="p">):</span>
                <span class="c1"># Handle categorical values by ordinal representation.</span>
                <span class="c1"># TODO(Yanase): Support one-hot representation.</span>
                <span class="n">lows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span><span class="p">)</span>
                <span class="n">highs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">dist</span><span class="o">.</span><span class="n">choices</span><span class="p">)</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist</span><span class="p">,</span> <span class="n">UniformDistribution</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist</span><span class="p">,</span> <span class="n">LogUniformDistribution</span><span class="p">):</span>
                <span class="n">lows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_to_cma_params</span><span class="p">(</span><span class="n">search_space</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">dist</span><span class="o">.</span><span class="n">low</span><span class="p">))</span>
                <span class="n">highs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_to_cma_params</span><span class="p">(</span><span class="n">search_space</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">dist</span><span class="o">.</span><span class="n">high</span><span class="p">))</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist</span><span class="p">,</span> <span class="n">DiscreteUniformDistribution</span><span class="p">):</span>
                <span class="n">r</span> <span class="o">=</span> <span class="n">dist</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="n">dist</span><span class="o">.</span><span class="n">low</span>
                <span class="n">lows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">dist</span><span class="o">.</span><span class="n">q</span><span class="p">)</span>
                <span class="n">highs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">r</span> <span class="o">+</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">dist</span><span class="o">.</span><span class="n">q</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist</span><span class="p">,</span> <span class="n">IntUniformDistribution</span><span class="p">):</span>
                <span class="n">lows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dist</span><span class="o">.</span><span class="n">low</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span>
                <span class="n">highs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dist</span><span class="o">.</span><span class="n">high</span> <span class="o">+</span> <span class="mf">0.5</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;The distribution </span><span class="si">{}</span><span class="s2"> is not implemented.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">dist</span><span class="p">))</span>

        <span class="c1"># Set initial params.</span>
        <span class="n">initial_cma_params</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">param_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_param_names</span><span class="p">:</span>
            <span class="n">initial_cma_params</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_to_cma_params</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_search_space</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">x0</span><span class="p">[</span><span class="n">param_name</span><span class="p">])</span>
            <span class="p">)</span>
        <span class="n">cma_option</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;BoundaryHandler&quot;</span><span class="p">:</span> <span class="n">cma</span><span class="o">.</span><span class="n">BoundTransform</span><span class="p">,</span>
            <span class="s2">&quot;bounds&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">lows</span><span class="p">,</span> <span class="n">highs</span><span class="p">],</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="n">cma_stds</span><span class="p">:</span>
            <span class="n">cma_option</span><span class="p">[</span><span class="s2">&quot;CMA_stds&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">cma_stds</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_param_names</span><span class="p">]</span>

        <span class="n">cma_opts</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">cma_option</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_es</span> <span class="o">=</span> <span class="n">cma</span><span class="o">.</span><span class="n">CMAEvolutionStrategy</span><span class="p">(</span><span class="n">initial_cma_params</span><span class="p">,</span> <span class="n">sigma0</span><span class="p">,</span> <span class="n">cma_opts</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tell</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trials</span><span class="p">,</span> <span class="n">study_direction</span><span class="p">):</span>
        <span class="c1"># type: (List[FrozenTrial], StudyDirection) -&gt; int</span>

        <span class="n">complete_trials</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_collect_target_trials</span><span class="p">(</span><span class="n">trials</span><span class="p">,</span> <span class="n">target_states</span><span class="o">=</span><span class="p">{</span><span class="n">TrialState</span><span class="o">.</span><span class="n">COMPLETE</span><span class="p">})</span>

        <span class="n">popsize</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_es</span><span class="o">.</span><span class="n">popsize</span>
        <span class="n">generation</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">complete_trials</span><span class="p">)</span> <span class="o">//</span> <span class="n">popsize</span>
        <span class="n">last_told_trial_number</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">generation</span><span class="p">):</span>
            <span class="n">xs</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">ys</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">complete_trials</span><span class="p">[</span><span class="n">i</span> <span class="o">*</span> <span class="n">popsize</span> <span class="p">:</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">popsize</span><span class="p">]:</span>
                <span class="n">x</span> <span class="o">=</span> <span class="p">[</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_to_cma_params</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_search_space</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">t</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="n">name</span><span class="p">])</span>
                    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_param_names</span>
                <span class="p">]</span>
                <span class="n">xs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
                <span class="n">ys</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
                <span class="n">last_told_trial_number</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">number</span>
            <span class="k">if</span> <span class="n">study_direction</span> <span class="o">==</span> <span class="n">StudyDirection</span><span class="o">.</span><span class="n">MAXIMIZE</span><span class="p">:</span>
                <span class="n">ys</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">y</span> <span class="k">if</span> <span class="n">y</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">y</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">ys</span><span class="p">]</span>

            <span class="c1"># Calling `ask` is required to avoid RuntimeError which claims that `tell` should only</span>
            <span class="c1"># be called once per iteration.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_es</span><span class="o">.</span><span class="n">ask</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_es</span><span class="o">.</span><span class="n">tell</span><span class="p">(</span><span class="n">xs</span><span class="p">,</span> <span class="n">ys</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">last_told_trial_number</span>

    <span class="k">def</span> <span class="nf">ask</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trials</span><span class="p">,</span> <span class="n">last_told_trial_number</span><span class="p">):</span>
        <span class="c1"># type: (List[FrozenTrial], int) -&gt; Dict[str, Any]</span>

        <span class="n">individual_index</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_collect_target_trials</span><span class="p">(</span><span class="n">trials</span><span class="p">,</span> <span class="n">last_told_trial_number</span><span class="p">))</span>
        <span class="n">popsize</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_es</span><span class="o">.</span><span class="n">popsize</span>

        <span class="c1"># individual_index may exceed the population size due to the parallel execution of multiple</span>
        <span class="c1"># trials. In such cases, `cma.cma.CMAEvolutionStrategy.ask` is called multiple times in an</span>
        <span class="c1"># iteration, and that may affect the optimization performance of CMA-ES.</span>
        <span class="c1"># In addition, please note that some trials may suggest the same parameters when multiple</span>
        <span class="c1"># samplers invoke this method simultaneously.</span>
        <span class="k">while</span> <span class="n">individual_index</span> <span class="o">&gt;=</span> <span class="n">popsize</span><span class="p">:</span>
            <span class="n">individual_index</span> <span class="o">-=</span> <span class="n">popsize</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_es</span><span class="o">.</span><span class="n">ask</span><span class="p">()</span>
        <span class="n">cma_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_es</span><span class="o">.</span><span class="n">ask</span><span class="p">()[</span><span class="n">individual_index</span><span class="p">]</span>

        <span class="n">ret_val</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_param_names</span><span class="p">,</span> <span class="n">cma_params</span><span class="p">):</span>
            <span class="n">ret_val</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_to_optuna_params</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_search_space</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ret_val</span>

    <span class="k">def</span> <span class="nf">_is_compatible</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial</span><span class="p">):</span>
        <span class="c1"># type: (FrozenTrial) -&gt; bool</span>

        <span class="c1"># Thanks to `intersection_search_space()` function, in sequential optimization,</span>
        <span class="c1"># the parameters of complete trials are always compatible with the search space.</span>
        <span class="c1">#</span>
        <span class="c1"># However, in distributed optimization, incompatible trials may complete on a worker</span>
        <span class="c1"># just after an intersection search space is calculated on another worker.</span>

        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">distribution</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_search_space</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">trial</span><span class="o">.</span><span class="n">params</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span>

            <span class="n">distributions</span><span class="o">.</span><span class="n">check_distribution_compatibility</span><span class="p">(</span><span class="n">distribution</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">distributions</span><span class="p">[</span><span class="n">name</span><span class="p">])</span>
            <span class="n">param_value</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
            <span class="n">param_internal_value</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">to_internal_repr</span><span class="p">(</span><span class="n">param_value</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">distribution</span><span class="o">.</span><span class="n">_contains</span><span class="p">(</span><span class="n">param_internal_value</span><span class="p">):</span>
                <span class="k">return</span> <span class="kc">False</span>

        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_collect_target_trials</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trials</span><span class="p">,</span> <span class="n">last_told</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span> <span class="n">target_states</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># type: (List[FrozenTrial], int, Optional[Set[TrialState]]) -&gt; List[FrozenTrial]</span>

        <span class="n">target_trials</span> <span class="o">=</span> <span class="p">[</span><span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">trials</span> <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">number</span> <span class="o">&gt;</span> <span class="n">last_told</span><span class="p">]</span>
        <span class="n">target_trials</span> <span class="o">=</span> <span class="p">[</span><span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">target_trials</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_compatible</span><span class="p">(</span><span class="n">t</span><span class="p">)]</span>
        <span class="k">if</span> <span class="n">target_states</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">target_trials</span> <span class="o">=</span> <span class="p">[</span><span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">target_trials</span> <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">state</span> <span class="ow">in</span> <span class="n">target_states</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">target_trials</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_to_cma_params</span><span class="p">(</span><span class="n">search_space</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">optuna_param_value</span><span class="p">):</span>
        <span class="c1"># type: (Dict[str, BaseDistribution], str, Any) -&gt; float</span>

        <span class="n">dist</span> <span class="o">=</span> <span class="n">search_space</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist</span><span class="p">,</span> <span class="n">LogUniformDistribution</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">optuna_param_value</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist</span><span class="p">,</span> <span class="n">DiscreteUniformDistribution</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">optuna_param_value</span> <span class="o">-</span> <span class="n">dist</span><span class="o">.</span><span class="n">low</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist</span><span class="p">,</span> <span class="n">CategoricalDistribution</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">dist</span><span class="o">.</span><span class="n">choices</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">optuna_param_value</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">optuna_param_value</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_to_optuna_params</span><span class="p">(</span><span class="n">search_space</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">cma_param_value</span><span class="p">):</span>
        <span class="c1"># type: (Dict[str, BaseDistribution], str, float) -&gt; Any</span>

        <span class="n">dist</span> <span class="o">=</span> <span class="n">search_space</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist</span><span class="p">,</span> <span class="n">LogUniformDistribution</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">cma_param_value</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist</span><span class="p">,</span> <span class="n">DiscreteUniformDistribution</span><span class="p">):</span>
            <span class="n">v</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">cma_param_value</span> <span class="o">/</span> <span class="n">dist</span><span class="o">.</span><span class="n">q</span><span class="p">)</span> <span class="o">*</span> <span class="n">dist</span><span class="o">.</span><span class="n">q</span> <span class="o">+</span> <span class="n">dist</span><span class="o">.</span><span class="n">low</span>
            <span class="c1"># v may slightly exceed range due to round-off errors.</span>
            <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">dist</span><span class="o">.</span><span class="n">low</span><span class="p">),</span> <span class="n">dist</span><span class="o">.</span><span class="n">high</span><span class="p">))</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist</span><span class="p">,</span> <span class="n">IntUniformDistribution</span><span class="p">):</span>
            <span class="n">r</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">round</span><span class="p">((</span><span class="n">cma_param_value</span> <span class="o">-</span> <span class="n">dist</span><span class="o">.</span><span class="n">low</span><span class="p">)</span> <span class="o">/</span> <span class="n">dist</span><span class="o">.</span><span class="n">step</span><span class="p">)</span>
            <span class="n">v</span> <span class="o">=</span> <span class="n">r</span> <span class="o">*</span> <span class="n">dist</span><span class="o">.</span><span class="n">step</span> <span class="o">+</span> <span class="n">dist</span><span class="o">.</span><span class="n">low</span>
            <span class="k">return</span> <span class="n">v</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist</span><span class="p">,</span> <span class="n">CategoricalDistribution</span><span class="p">):</span>
            <span class="n">v</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">cma_param_value</span><span class="p">))</span>
            <span class="k">return</span> <span class="n">dist</span><span class="o">.</span><span class="n">choices</span><span class="p">[</span><span class="n">v</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">cma_param_value</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../../../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>